package com.common.ex;

import com.common.ex.myException.CreateUserDirectoryException;
import com.common.ex.myException.FileUploadException;
import com.common.ex.myException.JwtParseException;
import com.common.ex.myException.MyException;
import io.jsonwebtoken.ExpiredJwtException;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import com.common.result.JsonResult;
import com.common.result.ResultEnum;
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler
    public JsonResult doUserExceptionHandler(UserException userException){
        return exception2Json(userException);
    }


    @ExceptionHandler(MethodArgumentNotValidException.class)
    public JsonResult doValidationExceptionHandler(MethodArgumentNotValidException methodArgumentNotValidException){
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode(41003);
        jsonResult.setMessage(methodArgumentNotValidException.getFieldError().getDefaultMessage());
        //System.out.println("我是filedError："+methodArgumentNotValidException.getFieldError());
        return jsonResult;
    }
    @ExceptionHandler(JwtStorageException.class)
    public JsonResult doJwtExceptionHandler(JwtStorageException jwtStorageException){
        return new JsonResult(ResultEnum.REDIS_STORAGE_JWT_ERROR);
    }
    @ExceptionHandler(IllegalArgumentException.class)
    public JsonResult doJwtIllegalArgumentExceptionHandler(IllegalArgumentException illegalArgumentException){
        return new JsonResult("JWT String argument cannot be null or empty.");

    }
    @ExceptionHandler(ExpiredJwtException.class)
    public JsonResult doJwtExpiredJwtExceptionHandler(ExpiredJwtException e){
        return new JsonResult("token已经过期");
    }

    @ExceptionHandler(JwtParseException.class)
    public JsonResult doJwtParseExceptionHandler(JwtParseException e){
        return new JsonResult("JWT解析失败");
    }


    public JsonResult exception2Json(MyException myException){
        log.info("2json:myException:{}",myException);
        log.info("2json:message:{}",myException.getMessage());
        log.info("2json:code:{}",myException.getErrorCode());
        JsonResult jsonResult=new JsonResult();
        jsonResult.setData(myException.getMessage());
        jsonResult.setCode(myException.getErrorCode());
        return jsonResult;
    }

//47000文件相关
    //注册时创建文件失败
    @ExceptionHandler(CreateUserDirectoryException.class)
    public JsonResult doCreateUserDirectoryExceptionHandler(CreateUserDirectoryException ex){
        log.info("ex:创建用户目录失败:{}",ex);

        JsonResult jsonResult = exception2Json(ex);

        return jsonResult;

    }
    @ExceptionHandler(FileUploadException.class)
    public JsonResult doFileUploadExceptionHandler(FileUploadException ex){
        log.info("ex:文件上传失败:{}",ex);
        JsonResult jsonResult = exception2Json(ex);
        return jsonResult;

    }

//    @ExceptionHandler
//    public JsonResult doOtherExceptionHandler(Throwable throwable){
//        return new JsonResult(ResultEnum.INTERNET_ERROR);
//
//    }
}
